package com.shushan.login.bean;

import android.util.Log;

import com.android.net.bean.NetBaseBean;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
import com.shushan.base.Constants;
import com.shushan.util.SharedPreferencesUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: ChildBean
 * @Desciption: 小孩bean
 * @author: yichaohua
 * @date: 2018-01-24
 */
@DatabaseTable(tableName = "children_table")
public class ChildBean extends NetBaseBean {

    private static final String TAG = "ChildBean";

    @DatabaseField
    public String childAvatar;//孩子头像
    @DatabaseField
    public String childName;// 孩子姓名
    @DatabaseField
    public String childPassword;//孩子密码
    @DatabaseField
    public int stars;//学贝数
    @DatabaseField
    public int gradeId;// 孩子年级id
    @DatabaseField
    public String gradeName;//年级名称
    @DatabaseField(id = true)
    public int id;
    @DatabaseField
    public String semester;//UP 上学期 DOWN 下学期

    private List<ChildBookBean> bookBeanList = new ArrayList<ChildBookBean>();

    @Override
    public void initByJson(JSONObject jsonObject) throws JSONException {
        childAvatar = jsonObject.optString("child_avatar","");
        childName = jsonObject.optString("child_name","");
        childPassword = jsonObject.optString("child_password","");

        stars = jsonObject.optInt("stars",0);
        gradeId = jsonObject.optInt("grade_id",1);
        gradeName = jsonObject.optString("grade_name","一年级");
        id = jsonObject.optInt("id",-1);
        semester = jsonObject.optString("semester","UP");

        JSONObject progressJson;
        if(jsonObject.has("progress")){
            progressJson = jsonObject.optJSONObject("progress");
        }else {
            progressJson = jsonObject.optJSONObject("outline_info");
        }

        if(progressJson != null){
            JSONObject chineseJson = progressJson.optJSONObject(Constants.SUBJECT_CHINESE_ID + "");
            if(chineseJson != null && chineseJson.length() > 0){
                ChildBookBean childBookBean = new ChildBookBean();
                childBookBean.subject_id = Constants.SUBJECT_CHINESE_ID;
                childBookBean.initByJson(chineseJson);
                bookBeanList.add(childBookBean);
            }

            JSONObject mathJson = progressJson.optJSONObject(Constants.SUBJECT_MACH_ID + "");
            if(mathJson != null && mathJson.length() > 0){
                ChildBookBean childBookBean = new ChildBookBean();
                childBookBean.subject_id = Constants.SUBJECT_MACH_ID;
                childBookBean.initByJson(mathJson);
                bookBeanList.add(childBookBean);
            }

            JSONObject englishJson = progressJson.optJSONObject(Constants.SUBJECT_ENGLISH_ID + "");
            if(englishJson != null && englishJson.length() > 0){
                ChildBookBean childBookBean = new ChildBookBean();
                childBookBean.subject_id = Constants.SUBJECT_ENGLISH_ID;
                childBookBean.initByJson(englishJson);
                bookBeanList.add(childBookBean);
            }
        }
    }

    public List<ChildBookBean> getBookBeanList() {
        return bookBeanList;
    }

    public void setBookBeanList(List<ChildBookBean> bookBeanList) {
        this.bookBeanList = bookBeanList;
    }
}
